<template>

    <!-- 添加或修改对话框 -->
    <lam-dialog :title="title" :visible.sync="open" width="680px" append-to-body @close="cancel">
      <el-form :ref="formRef" :model="form" :rules="rules" label-width="100px" :disabled="formDisabled">
        <el-row>
          <el-col :span="12">
            <el-form-item label="编码" prop="code">
              <el-input v-model="form.code" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="表名" prop="tableName">
              <span slot="label">
                表名<el-tooltip content="数据库表全名，例: sys_user" placement="top"><i class="el-icon-question"></i></el-tooltip>
              </span>
              <el-input v-model="form.tableName" clearable placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item prop="uniqueField">
              <span slot="label">
                唯一字段<el-tooltip content="新增修改时要校验唯一的字段" placement="top"><i class="el-icon-question"></i></el-tooltip>
              </span>
              <el-input v-model="form.uniqueField" type="textarea" autosize placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="queryField">
              <span slot="label">
                查询字段<el-tooltip content="修改时用于定位当前记录的字段，一般为主键id，若不涉及修改操作，可以为空" placement="top"><i class="el-icon-question"></i></el-tooltip>
              </span>
              <el-input v-model="form.queryField" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <lam-dict-select type="radio" v-model="form.status" dictCode="status_type" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item prop="conditionSql">
              <span slot="label">
                查询条件<el-tooltip content="可以自定义SQL，变量用{XXX}，若变量类型为数字类型，则变量加上后缀: XXX_NUM" placement="top"><i class="el-icon-question"></i></el-tooltip>
              </span>
              <el-input v-model="form.conditionSql" type="textarea" autosize placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类型" prop="type">
              <lam-dict-select type="radio" v-model="form.type" dictCode="config_type" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" autosize placeholder="" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="loading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </lam-dialog>

</template>

<script>
import { EditMixin } from '@/components/mixins/EditMixin';

export default {
  name: "EditTableUnique",
  mixins:[EditMixin],
  data() {
    return {
      url: {
        base: '/sys/tableUnique'
      },
      // 表单校验
      rules: {
        code: [{ required: true, message: "唯一编码不能为空", trigger: "blur" }, { validator: this.codeUniqueValidator, trigger: 'blur' }],
        tableName: [{ required: true, message: "表名不能为空", trigger: "blur" }],
        uniqueField: [{ required: true, message: "唯一字段不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        type: [{ required: true, message: "类型不能为空", trigger: "blur" }],
      }
    };
  },
  created() {
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        code: null,
        tableName: null,
        uniqueField: null,
        queryField: 'id',
        conditionSql: null,
        status: 1,
        remark: null
      };
    },

    codeUniqueValidator(rule, value, callback) {
      let param = this.operType=='update'?{code: value, id: this.form.id}:{code: value };
      this.checkUniqueAsyn(value, callback, 'tableUniqueCode', param);
    },

    /** 提交按钮 */
    submitForm(){
      this.saveForm(this.url.base);
    }

  }
};
</script>
